﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OrgTree.aspx.cs" Inherits="Honestysoft.Baseframework.WebUI.SystemManage.Organization.OrgTree" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>    
	<link rel="stylesheet" href="../../Scripts/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css" />
	<script type="text/javascript" src="../../Scripts/zTree/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="../../Scripts/zTree/js/jquery.ztree.core-3.0.js"></script>
	<script type="text/javascript" src="../../Scripts/zTree/js/jquery.ztree.excheck-3.0.js"></script>
	<script type="text/javascript" src="../../Scripts/zTree/js/jquery.ztree.exedit-3.0.js"></script>


    <script type="text/javascript">
        <!--
        var setting = {
            view: {
		        selectedMulti: false
	        },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                url: "JSON/OrgTree.ashx",
                autoParam: ["id"],
                otherParam: { "Module": "1","FunctionID":"<%=Request["FunctionID"]+"" %>" },
                dataFilter: null
            },
            callback: {
                beforeDrag: beforeDrag,
                beforeDrop: beforeDrop,
                beforeClick: beforeClick,
				onClick: onClick
            }

        };
        function beforeClick(treeId, treeNode, clickFlag) {
            
		}
		function onClick(event, treeId, treeNode, clickFlag) {
            if(!treeNode.open){
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			    zTree.expandNode(treeNode);
            }
            window.parent.frames[treeNode.target].location.href=treeNode.url;
		}		

        function beforeDrag(treeId, treeNodes) {
            for (var i = 0, l = treeNodes.length; i < l; i++) {
                if (treeNodes[i].drag === false) {
                    return false;
                }
            }
            return true;
        }
        function beforeDrop(treeId, treeNodes, targetNode, moveType) {
                    //不能将公司移动到部门、员工下，不能将部门移动到员工下、不能将部门、员工移动到根下
                    var targetType = targetNode.nodeType;
                    var sourceType = treeNodes[0].nodeType;
                    if (sourceType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Root_Inner).ToString() %>'
                    || sourceType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Root_Outer).ToString() %>') {
                        alert("不能移动根节点！");
                        return false;
                    }
                     if (sourceType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.CompanyCategory).ToString() %>'){
                        alert("不能移动公司类别节点！");
                        return false;
                     }
                    if (sourceType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Company).ToString() %>') {
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Department).ToString() %>') {
                            alert("不能将公司移动到部门节点下！");
                            return false;
                        }
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Employee).ToString() %>') {
                            alert("不能将公司移动到人员节点下！");
                            return false;
                        }
                    }
                    if (sourceType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Department).ToString() %>') {
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.CompanyCategory).ToString() %>'){
                            alert("不能将部门移动到公司类别节点下！");
                            return false;
                         }
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Employee).ToString() %>') {
                            alert("不能将部门移动到人员节点下！");
                            return false;
                        }
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Root_Inner).ToString() %>'
                        || targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Root_Outer).ToString() %>') {
                            alert("不能将部门移动到根节点下！");
                            return false;
                        }
                    }
                    if (sourceType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Employee).ToString() %>') {
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Employee).ToString() %>') {
                            alert("不能将人员移动到人员节点下！");
                            return false;
                        }
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.CompanyCategory).ToString() %>'){
                            alert("不能将人员移动到公司类别节点下！");
                            return false;
                         }
                        if (targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Root_Inner).ToString() %>'
                        || targetType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Root_Outer).ToString() %>') {
                            alert("不能将人员移动到根节点下！");
                            return false;
                        }
                    }                    
                    $.ajax({
                        type: "POST",
                        url: "JSON/MoveOrganization.ashx?FromID=" + treeNodes[0].id + "&ToID=" + targetNode.id +"&randId=" + escape(new Date()),
                        data: "",
                        success: function (msg) {
                            alert(msg);
                        },
                        error:function(){
                            alert("调整组织机构归属节点失败！");
                        }
                    });
            return true;
        }
        function filter(treeId, parentNode, childNodes) {
//            if (!childNodes) return null;
//            for (var i = 0, l = childNodes.length; i < l; i++) {
//                childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
//            }
//            return childNodes;
        }

        $(document).ready(function () {
            $.fn.zTree.init($("#treeDemo"), setting);            
        });
        function addNode(nodeId, nodeText, ouType){
            var nodeURI = "";
            var iconUrl = "";
            if (ouType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.CompanyCategory).ToString() %>') {
                iconUrl = "../../images/folder1.gif";
                nodeURI = "Org_Company_List.aspx";
            }
            else if (ouType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Company).ToString() %>') {
                nodeURI = "CompanyInfo.aspx";
                iconUrl = "../../images/company.gif";
            }
            else if (ouType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Department).ToString() %>') {
                nodeURI = "DepartmentInfo.aspx";
                iconUrl = "../../images/department.gif"
            }
            else if (ouType == '<%=((int)Honestysoft.Baseframework.Model.EnumItem.OuType.Employee).ToString() %>') {
                nodeURI = "EmployeeInfo.aspx";
                iconUrl = "../../images/personal.gif";
            }
            nodeURI += "?OuType="+ouType+"&ID=" + nodeId + '&FunctionID=<%=Request["FunctionID"] %>';
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var zNodes =
			{ id:nodeId,  name:nodeText,icon:iconUrl,target:'ifrmInfo',url:nodeURI,isParent:false, open:true,nodeType:ouType};
            var selectedNodes = zTree.getSelectedNodes();           
			zTree.addNodes(selectedNodes[0], zNodes);
            var newnode = zTree.getNodeByParam("id",nodeId);
            zTree.selectNode(newnode);
            window.parent.frames[newnode.target].location.href=newnode.url;
        }
        function removeSelectedNode() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var selectedNodes = zTree.getSelectedNodes()[0];
            var newnode = selectedNodes.getParentNode();
            zTree.removeNode(selectedNodes);           
            zTree.selectNode(newnode);
            window.parent.frames[newnode.target].location.href=newnode.url;
            
        }
        function removeChildNode(arrId) {
            if (arrId == null || arrId.length == 0) { return; }
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var selectedNodes = zTree.getSelectedNodes()[0];
            var nodes = selectedNodes.children;
            if (nodes && nodes.length > 0) {
                for (var i = nodes.length - 1; i >= 0; i--) {
                    for (var j = 0; j < arrId.length; j++) {
                        if (nodes[i].id == arrId[j]) {
                            zTree.removeNode(nodes[i]);           
                            break;
                        }
                    }
                }
            }
        }
        function getIconUrl(iconUrl){
            if(iconUrl == "icon-employee-disable"){
                return "../../images/personal_disable.gif";
            }
            else if(iconUrl == "icon-employee"){
                return "../../images/personal.gif";
            }
            else if(iconUrl == "icon-department-disable"){
                return "../../images/department_disable.gif";
            }
            else if(iconUrl == "icon-department"){
                return "../../images/department.gif";
            }
            else if(iconUrl == "icon-company-disable"){
                return "../../images/company_disable.gif";
            }
            else if(iconUrl == "icon-company"){
                return "../../images/company.gif";
            }
            else if(iconUrl == "icon-companycategory-disable"){
                return "../../images/folder1_disable.gif";
            }
            else if(iconUrl == "icon-companycategory"){
                return "../../images/folder1.gif";
            }
            else{
                return "";
            }
        }

        function editSelectedNodeCls(iconUrl){
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var selectedNodes = zTree.getSelectedNodes()[0];
            if (selectedNodes) {
                selectedNodes.icon = getIconUrl(iconUrl);
                zTree.updateNode(selectedNodes);
            }
        }
        function editSelectedNode(nodeText) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var selectedNodes = zTree.getSelectedNodes()[0];
            if (selectedNodes) {
                selectedNodes.name = nodeText;
                zTree.updateNode(selectedNodes);
            }
        }
        function editChildNode(nodeId, nodeText) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var selectedNodes = zTree.getSelectedNodes()[0];
            var nodes = selectedNodes.children;
            if (nodes && nodes.length > 0) {
                for (var i = nodes.length - 1; i >= 0; i--) {
                    if (nodes[i].id == nodeId) {
                        nodes[i].name = nodeText;
                        zTree.updateNode(nodes[i]);
                        break;
                    }
                }
            }
        }
        function editChildNodeCls(arrNodeId, iconUrl) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var selectedNodes = zTree.getSelectedNodes()[0];
            var nodes = selectedNodes.children;
            if (nodes && nodes.length > 0) {
                for (var i = nodes.length - 1; i >= 0; i--) {
                    for (var j = 0; j < arrNodeId.length; j++) {
                        if (nodes[i].id == arrNodeId[j]) {
                            nodes[i].icon = getIconUrl(iconUrl);
                            zTree.updateNode(nodes[i]);
                            break;
                        }
                    }                    
                }
            }
        }
	</script>
</head>
<body>
    <form id="form1" runat="server">
     <div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
    </form>
</body>
</html>
